﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace NFox.Pyeditor.Runtime
{
    public class TreeListView : TreeView
    {

        public GridViewColumnCollection Columns { get; set; }
            = new GridViewColumnCollection();

        //这两个默认的是TreeViewItem
        protected override DependencyObject GetContainerForItemOverride()//创建或标识用于显示指定项的元素。 
        {
            return new TreeListViewItem();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)//确定指定项是否是（或可作为）其自己的 ItemContainer
        {
            //return item is TreeListViewItem;
            bool _isTreeLVI = item is TreeListViewItem;
            return _isTreeLVI;
        }

    }

    public class TreeListViewItem : TreeViewItem
    {

        private int _level = -1;
        /// <summary>
        /// hierarchy 
        /// </summary>
        public int Level
        {
            get
            {
                if (_level == -1)
                {
                    TreeListViewItem parent =
                        ItemsControl.ItemsControlFromItemContainer(this) as TreeListViewItem;//返回拥有指定的容器元素中 ItemsControl 。 
                    _level = (parent != null) ? parent.Level + 1 : 0;
                }
                return _level;
            }
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new TreeListViewItem();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            //return item is TreeListViewItem;
            bool _isITV = item is TreeListViewItem;
            return _isITV;
        }
        
    }

    public interface ITreeListItemModel
    {

        bool HasChildren { get; }

        IEnumerable<ITreeListItemModel> GetChildren();

    }

    public class TreeListItemViewModel
    {

        private static TreeListItemViewModel _visual= new TreeListItemViewModel(null);

        public ITreeListItemModel Value { get; }

        public ObservableCollection<TreeListItemViewModel> Children { get; }
            = new ObservableCollection<TreeListItemViewModel>();

        internal TreeListItemViewModel(ITreeListItemModel value)
        {
            Value = value;
            if (Value != null)
            {
                if (Value.HasChildren)
                    Children.Add(_visual);
            }
        }


        bool _isExpanded;

        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                _isExpanded = value;
                if (Value != null && Value.HasChildren && Children.Count == 1 && Children[0] == _visual)
                {
                    Children.Clear();
                    foreach (var item in Value.GetChildren())
                    {
                        Children.Add(new TreeListItemViewModel(item));
                    }

                }
            }
        }

    }

    public class LevelToIndentConverter : IValueConverter
    {
        public object Convert(object o, Type type, object parameter, CultureInfo culture)
        {
            return new Thickness((int)o * c_IndentSize, 0, 0, 0);
        }

        public object ConvertBack(object o, Type type, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        private const double c_IndentSize = 19.0;
    }

}
